package org.mymail.mail;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;

import java.util.ArrayList;

import java.util.Iterator;

import javax.mail.Address;
import javax.mail.internet.InternetAddress;
import noNamespace.FreskoSubscribersDocument;
import org.apache.xmlbeans.XmlException;
import noNamespace.FreskoSubscribersDocument.FreskoSubscribers;
import noNamespace.FreskoSubscribersDocument.FreskoSubscribers.Subscriber;
import noNamespace.FreskoSubscribersDocument.FreskoSubscribers.Subscriber.SubscribeType;

public class UpdateSubscribers {

	static FreskoSubscribersDocument recipientsDoc;
	static FreskoSubscribers.Subscriber sub[];

	static {
		try {
			recipientsDoc = noNamespace.FreskoSubscribersDocument.Factory
					.parse(new File(MailConstants.recipientsXMLLoc));
			FreskoSubscribers s = recipientsDoc.getFreskoSubscribers();
			sub = s.getSubscriberArray();
		} catch (XmlException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void removeSubscriber(Address address, int mailFwdType) {
		InternetAddress address2 = (InternetAddress) address;
		for (int i = 0; i < sub.length; i++) {
			FreskoSubscribers.Subscriber subscriber = sub[i];
			if (subscriber.getId().equalsIgnoreCase(address2.getAddress())) {
				FreskoSubscribers.Subscriber.SubscribeType subType[] = subscriber
						.getSubscribeTypeArray();
				for (int j = 0; j < subType.length; j++) {
					FreskoSubscribers.Subscriber.SubscribeType subscribeType = subType[j];
					if (subscribeType.getId() == mailFwdType) {
						subscribeType.setId(-1);
						try {
							recipientsDoc.save(new File(
									MailConstants.recipientsXMLLoc));
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}

			}
		}

	}

	public static void addSubscribers(BufferedReader in, String from) {
		ArrayList<SubscriberDetails> subscriberDet = new ArrayList<SubscriberDetails>();
		try {
			String str = "";
			while (str != null) {
				str = in.readLine();
				subscriberDet = processSubscribers(from, subscriberDet, str);
			}
			addToSource(subscriberDet);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void addToSource(ArrayList<SubscriberDetails> subscriberDet) {

		String subBy = "";
		for (Iterator<SubscriberDetails> iterator = subscriberDet.iterator(); iterator
				.hasNext();) {
			SubscriberDetails subscriberDetails = (SubscriberDetails) iterator
					.next();
			Subscriber sub = recipientsDoc.getFreskoSubscribers()
					.addNewSubscriber();
			SubscribeType subtype = sub.addNewSubscribeType();
			sub.setName(subscriberDetails.getName());
			sub.setId(subscriberDetails.getMailID());
			// Append name : mailId as 'subscribed by' in xml file
			if (!subBy.contains(subscriberDetails.getSubscribedBy())) {
				subBy = appendSubByDetails(subscriberDetails.getSubscribedBy());
			}
			sub.setSubscribedBy(subBy);
			subtype.setId(1);
			subtype.setLastReceived(-1);
		}

		try {
			recipientsDoc.save(new File(MailConstants.recipientsXMLLoc));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static String appendSubByDetails(String subscribedBy) {

		for (int i = 0; i < sub.length; i++) {
			Subscriber sub2 = sub[i];
			if (sub2.getId().equalsIgnoreCase(subscribedBy)) {
				return sub2.getName() + " : " + subscribedBy;
			}
		}

		return subscribedBy;
	}

	public static ArrayList<SubscriberDetails> processSubscribers(String from,
			ArrayList<SubscriberDetails> subscriberDet, String str) {
		if (str != null && (str.indexOf("@") > 0)) {
			if (str.indexOf("-") > 0 && (!(str.indexOf("<") > 0))
					&& (!(str.indexOf(">") > 0))) {
				String[] details = str.split("-");
				String name = details[0].trim();
				String mailId = details[1].trim();
				if (mailId.contains("@") && newSubscriber(mailId)) {
					subscriberDet
							.add(new SubscriberDetails(name, mailId, from));
				}
			} else if (str.indexOf("@") > 0 && (!(str.indexOf("<") > 0))
					&& (!(str.indexOf(">") > 0))) {
				String name = "";
				String mailId = str;
				if (mailId.contains("@") && newSubscriber(mailId)) {
					subscriberDet
							.add(new SubscriberDetails(name, mailId, from));
				}
			}
		}
		return subscriberDet;
	}

	private static boolean newSubscriber(String mailId) {

		for (int i = 0; i < sub.length; i++) {
			Subscriber sub2 = sub[i];
			if (sub2.getId().equalsIgnoreCase(mailId)) {
				return false;
			}
		}

		return true;
	}
}